package com.shou.zhnfyxt.controller;



import com.shou.zhnfyxt.entity.HouseIssueApprovalBasicInfo;
import com.shou.zhnfyxt.entity.HousePic;
import com.shou.zhnfyxt.entity.R;
import com.shou.zhnfyxt.service.HouseIssueApprovalBasicInfoService;
import com.shou.zhnfyxt.service.IHousePicService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author JCY
 * @since 2022-12-19
 */
@RestController
@RequestMapping("/house/houseIssueApprovalBasicInfo")
public class HouseIssueApprovalBasicInfoController {
    final
    HouseIssueApprovalBasicInfoService houseIssueApprovalBasicInfoService;

    final
    IHousePicService housePicService;

    public HouseIssueApprovalBasicInfoController(HouseIssueApprovalBasicInfoService houseIssueApprovalBasicInfoService, IHousePicService housePicService) {
        this.houseIssueApprovalBasicInfoService = houseIssueApprovalBasicInfoService;
        this.housePicService = housePicService;
    }

    @PostMapping("issueToApproval")
    public R<String> issueToApproval(@RequestBody Map<String,Object> map) {
        System.out.println(map);

        HouseIssueApprovalBasicInfo houseIssueApprovalBasicInfo = new HouseIssueApprovalBasicInfo();
        String roomtext = (String) map.get("roomtext");
        String estatetext = (String) map.get("estatetext");
        String estate = (String) map.get("estate");
        String address = (String) map.get("address");
        Integer price = (Integer) map.get("price");
        String phone = (String) map.get("phone");
        String city = (String) map.get("city");
        String firstLocation = (String) map.get("firstLocation");
        String secondLocation = (String) map.get("secondLocation");
        String thirdLocation = (String) map.get("thirdLocation");

        System.out.println(estate);
        houseIssueApprovalBasicInfo.setIssueAddress(address);
        houseIssueApprovalBasicInfo.setIssueRoomDescribe(roomtext);
        houseIssueApprovalBasicInfo.setIssueEstate(estate);
        houseIssueApprovalBasicInfo.setIssueEstateDescribe(estatetext);
        houseIssueApprovalBasicInfo.setIssuePhone(phone);
        houseIssueApprovalBasicInfo.setIssuePrice(price);
        houseIssueApprovalBasicInfo.setIssueCity(city);
        houseIssueApprovalBasicInfo.setIssueFirstLocation(firstLocation);
        houseIssueApprovalBasicInfo.setIssueSecondLocation(secondLocation);
        houseIssueApprovalBasicInfo.setIssueThirdLocation(thirdLocation);


        List<String> option = (List<String>) map.get("option");
        String options = option.get(0) + option.get(1) + option.get(2);
        houseIssueApprovalBasicInfo.setIssueOption(options);
        //处理面积
        Object area = map.get("area");
        area = Double.valueOf(area.toString());
        houseIssueApprovalBasicInfo.setIssueArea((Double) area);
        houseIssueApprovalBasicInfoService.save(houseIssueApprovalBasicInfo);
        Integer approvalId = houseIssueApprovalBasicInfo.getApprovalId();
        //处理图片
        //使用类型检查规避强制类型转换的警告
        List<?> pic = null;
        if (map.get("pic") instanceof ArrayList<?>) {
            pic = (List<?>) map.get("pic");
        }
        List<HousePic> housePics = new ArrayList<>();
        for (int i = 0; i < Objects.requireNonNull(pic).size() ; i++ ) {
            HousePic housePic = new HousePic();
            housePic.setApprovalPicId(approvalId);
            housePic.setPicAddress((String) pic.get(i));
            housePics.add(housePic);
        }
        housePicService.saveBatch(housePics);
        return R.success("success");
    }


}

